package com.ramble.authorityservice.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;

@Api(tags = "测试  api")
@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private DataSource dataSource;

    @Value("${server.port}")
    private String serverPort;

    @ApiOperation("hello接口")
    @GetMapping("/hello")
    public String hello() {
        return serverPort + "     AuthorizationService - test - hello";
    }


    @PostMapping("/client/")
    public BaseClientDetails createClient() {
        JdbcClientDetailsService service = new JdbcClientDetailsService(dataSource);
        service.setPasswordEncoder(passwordEncoder);
        BaseClientDetails clientDetails = new BaseClientDetails();
        service.addClientDetails(clientDetails);
        return clientDetails;
    }

}
